package co.fitstart.market.web.controller.course.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.entity.market.MarketDailyCourseware;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.CoursewareService;
import co.fitstart.market.web.interceptor.MobileDetect;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;
import co.fitstart.market.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/courses/{monthlyId}/batches/{courseId}/units/{unitId}")
public class CourseUnitRestController {
    
    @Autowired
    private CoursewareService coursewareService;
    
    private static final Logger logger = LoggerFactory.getLogger(CourseUnitRestController.class);
    
    /**
     * 
     * 开始运动
     * 
     * @param unitId
     * @return
     */
    @RequestMapping(value = "/start", method = RequestMethod.POST)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String start(@Secure User user, @PathVariable("unitId") Long unitId) {
        
        Result result = new Result();
        
        // 创建单元的训练记录，成功后，
        // 重定向到该单元的教学课程页面
        try {
            
            MarketDailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(unitId);
            coursewareService.startTrain(dailyCourseware, user);
            
            result.success().message("开始运动成功");
            
        } catch (Exception e) {
            logger.error("start - ", e);
            result.fail("500").message("开始运动失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * 完成运动8
     * 
     * @param unitId
     * @return
     */
    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String complete(@Secure User user, @PathVariable("unitId") Long unitId) {
        
        Result result = new Result();
        
        // 更新该上课记录为已完成，并且生成积分
        // 重定向到该单元的奖状页
        
        try {
            
            MarketDailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(unitId);
            coursewareService.finishTrain(dailyCourseware, user);
            result.success().message("结束运动成功");
            
        } catch (Exception e) {
            logger.error("complete - ", e);
            result.fail().message("结束运动失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
}
